import { Injectable, HttpStatus, HttpException } from '@nestjs/common';
import { Equal, Repository } from 'typeorm'
import { InjectRepository } from '@nestjs/typeorm'
import { AuthService } from '../auth/auth.service'
import { User } from '../../entities/user.entity'
import { UpdateUserDto } from './dtos/update-user.dto'


@Injectable()
export class UserService {
    constructor(
        @InjectRepository(User)
        private readonly userRepository: Repository<User>,
        private readonly authService: AuthService
    ) { }

    async getInfo(userId: string) {
        return await this.userRepository.findOne({ where: { id: userId } })
    }

    async getInfoById(id: string) {
        return await this.userRepository.findOne({ where: { id: id } })
    }

    async getInfoAll(page, limit, order) {
        const skip = (page - 1) * limit
        const take = limit
        const list = await this.userRepository.find({ skip, take, order, })
        const total = await this.userRepository.count()

        return { list, page, limit, total }
    }

    async putInfo(userId: string, updateUserDto: UpdateUserDto) {
        // 将数据进行更新
        console.log(userId)
        console.log(updateUserDto)
        try {
            await this.userRepository.update(userId, updateUserDto)
            return '修改成功'
        }
        catch (e) {
            console.log(e.message)
            throw new HttpException('服务器内部错误', HttpStatus.BAD_REQUEST)
        }
        return '修改成功'
    }
}
